/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.ReportService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <dl>
 * <dd>描述: ~ ReportController</dd>
 * <dd>创建时间：  10:51 2021/3/8</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2021/3/8      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@RestController
@RequestMapping("report")
public class ReportController {

    @DubboReference
    private ReportService reportService;

    /**
     * @Author: guodong
     * @Date: 10:52 2021/3/8
     * @ClassName: ReportController
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 获取会员统计数据
     */
    @GetMapping("getMemberReport")
    public Result getMemberReport() {
        Map<String, Object> resultMap = new HashMap<>();
        //组装最近12个月的日期数据
        List<String> months = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -13);
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            String month = sdf.format(calendar.getTime());
            months.add(month);
        }
        resultMap.put("months", months);
        //查询最近12个月的会员统计数据
        List<Integer> memberCount = reportService.getMemberReport(months);
        resultMap.put("memberCount", memberCount);
        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, resultMap);
    }

    @GetMapping("getMemberReportDateRange")
    public Result getMemberReportDateRange(String start, String end) {

        String startStr = start.replace("-", ".");
        String endStr = end.replace("-", ".");
        String startSubs = startStr.substring(0, 7);
        String endSubs = endStr.substring(0, 7);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
        try {
            Date startTime = sdf.parse(startSubs);
            Date endTime = sdf.parse(endSubs);
            System.out.println("===========================");
            System.out.println(startTime + "" + endTime);
            System.out.println("===========================");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //Map<String, Object> resultMap = new HashMap<>();
        ////组装最近12个月的日期数据
        //List<String> months = new ArrayList<>();
        //Calendar calendar = Calendar.getInstance();
        //calendar.add(Calendar.MONTH, -13);
        //for (int i = 0; i < 12; i++) {
        //    calendar.add(Calendar.MONTH, 1);
        //    String month = sdf.format(calendar.getTime());
        //    months.add(month);
        //}
        //resultMap.put("months", months);
        ////查询最近12个月的会员统计数据
        //List<Integer> memberCount = reportService.getMemberReport(months);
        //resultMap.put("memberCount", memberCount);
        //return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, resultMap);
        return null;
    }

    /**
     * @Author: guodong
     * @Date: 8:58 2021/3/9
     * @ClassName: ReportController
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 查询套餐统计数据
     */
    @GetMapping("getSetmealReport")
    public Result getSetmealReport() {
        Map<String, Object> resultMap = reportService.getSetmealReport();
        return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, resultMap);
    }

    /**
     * @Author: guodong
     * @Date: 9:42 2021/3/9
     * @ClassName: ReportController
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 会员统计
     */
    @GetMapping("getBusinessReportData")
    public Result getBusinessReportData() {
        Map<String, Object> resultMap = reportService.getBusinessReportData();
        return new Result(true, MessageConst.GET_BUSINESS_REPORT_SUCCESS, resultMap);
    }
/**
 * @Author: wanghong
 * @Date: 9:09 2021/3/12
 * @Parms []
 * @ReturnType: com.itheima.health.entity.Result
 * @Description: 根据性别统计会员饼状图
*/
    @GetMapping("getSetmealReportBysex")
    public Result getSetmealReportBysex() {
        List<Map<String, Object>> list = reportService.getSetmealReportBySex();
        return new Result(true, MessageConst.ACTION_SUCCESS, list);
    }

    /**
     * @Author: wanghong
     * @Date: 11:20 2021/3/12
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 根据年龄统计会员饼状图
    */
    @GetMapping("getSetmealReportByAge")
    public Result getSetmealReportByAge(){
        List<Map<String, Object>> list = reportService.getSetmealReportByAge();
        return new Result(true, MessageConst.ACTION_SUCCESS, list);
    }

    /**
     * @Author: wanghong
     * @Date: 20:03 2021/3/12
     * @Parms [date01, date02]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 根据折线图显示日历插件
    */
    @GetMapping("getMemberReportOfDate")
    public Result getMemberReportOfDate(String start, String end) throws ParseException {
        Map<String, Object> resultMap = new HashMap<>();
        List<String> dateList = new ArrayList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date startTime = sdf.parse(start);
        Date endTime = sdf.parse(end);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);

        while (calendar.getTime().before(endTime)) {
            dateList.add(sdf.format(calendar.getTime()));
            calendar.add(Calendar.MONTH, 1);
        }

        resultMap.put("monthList", dateList);

        List<Integer> count = reportService.getMemberReport(dateList);
        resultMap.put("memberCount", count);

        return new Result(true, MessageConst.ACTION_SUCCESS, resultMap);
    }

}
